<?php

namespace app\store\controller\apps\bargain;

use app\store\controller\Controller;
use app\store\model\Goods as GoodsModel;
use app\store\model\bargain\Active as ActiveModel;
use think\Cache;
// use app\api\model\bargain\Active as ActiveModel;
use think\Request;
use think\Db;
/**
 * 砍价活动管理
 * Class Active
 * @package app\store\controller\apps\bargain
 */
class Active extends Controller
{
    /**
     * 砍价活动列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($search = '')
    {
    	
        $model = new ActiveModel;
        $list = $model->getList($search);
        // print_r($list);
        return $this->fetch('index', compact('list'));
    }
    
    
    // 批量设置活动的跳转广告 fujuhao 2020-04-28
    public function batch_set_jump_ads(){
    	$data = $this->request->param();
    	// return db('bargain_active')->where('active_id','<=',10002)->update($data);
    	return db('bargain_active')->where('active_id','>=', '1')->update($data);
    }

    /**
     * 新增砍价活动
     * @return array|bool|mixed
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        $model = new ActiveModel;
        $goodsModel = new GoodsModel;
        $active = $this->postData('active');
        $goods = GoodsModel::detail($active['goods_id']);
        $temp = explode("】",$goods['goods_name']);
        // 替换之前的
        if(count($temp) == 2){
            $up['goods_name'] = "【{$active['peoples']}刀砍成】".$temp[1];
        }else{
            $up['goods_name'] = "【{$active['peoples']}刀砍成】".$temp[0];
        }
        $goodsModel->save($up, ["goods_id" => $active['goods_id']]);
        // 新增记录
        if ($model->add($active)) {
        	// 清除缓存
			Cache::clear('activeList');
	
            return $this->renderSuccess('添加成功', url('apps.bargain.active/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新砍价活动
     * @param $active_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($active_id)
    {
    	// print_r(Cache::tag('activeList'));return;
        // 砍价活动详情
        $model = ActiveModel::detail($active_id);
        $goods = GoodsModel::detail($model['goods_id']);
        if (!$this->request->isAjax()) {
            // 获取商品详情
            return $this->fetch('edit', compact('model', 'goods'));
        }
        // 更新记录
        $active = $this->postData('active');
        if ($model->edit($active)) {
            $goodsModel = new GoodsModel;
            $temp = explode("】",$goods['goods_name']);
            // 替换之前的
            if(count($temp) == 2){
                $up['goods_name'] = "【{$active['peoples']}刀砍成】".$temp[1];
            }else{
                $up['goods_name'] = "【{$active['peoples']}刀砍成】".$temp[0];
            }
            $goodsModel->save($up, ["goods_id" => $model['goods_id']]);
        	// 清除缓存
        	Cache::clear('activeList');

            return $this->renderSuccess('更新成功', url('apps.bargain.active/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除砍价活动
     * @param $active_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($active_id)
    {
        // 砍价活动详情
        $model = ActiveModel::detail($active_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        // 清除缓存
        Cache::clear('activeList');

        return $this->renderSuccess('删除成功');
    }
	
}